# 转换操作
from __future__ import print_function
import torch
import numpy as np

# torch tensor 和numpy array共享底层内存空间，数据互通

a = torch.ones(5)
print(a)
b = a.numpy()
print(b)

# 修改
a.add_(1)
print(a)
print(b)
print("--------------")

# numpy转torch
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)

# 数据在不同设备的转移
if torch.cuda.is_available():
    print(1111111111)
    device = torch.device("cuda")
    # cpu上创建x，gpu上创建y
    x = torch.randn(1)
    y = torch.ones_like(x, device=device)
    # 将x转移到gpu
    x = x.to(device)
    z = x + y
    print(z)
    z.to("cpu", torch.double)
    print(z)
